package com.bean.sys;

import java.sql.SQLException;
import java.util.HashMap;

import com.Constants;
import com.util.StringHelper;
import com.datasource.*;


public class ResourceBean
{
	private int resId;
	
	private int parentId;
	
	private String resName;
	
	private int nodeType;
	
	private int modelId;
	
	private int position;
	
	private String smallImage = "";
	
	private String bigImage = "";
	
	private String linkValue;
	
	private String description;
	
	private String operation;
	
	public int add() throws SQLException
	{
		DSSession dss = DSManager.getDSSession();
		int n = 0;
		dss.beginTransaction();
		try
		{
			
			StringBuffer sql = new StringBuffer("INSERT INTO t_base_function" +
					"(parent_id,res_name,node_type,model_id,position,link_value,remark,operation) " +
					"VALUES(?,?,?,?,?,?,?,?)");
			Object [] prepareParam = new Object[8];
		
			prepareParam[0] = new Integer( parentId );
			prepareParam[1] = resName;
			prepareParam[2] = new Integer( nodeType );
			if( modelId <= 0 )
			{
				prepareParam[3] = new Integer( 1 );
			}else
			{
				prepareParam[3] = new Integer( modelId );
			}
			prepareParam[4] = new Integer( position );
			
			prepareParam[5] = linkValue;
			prepareParam[6] = description;
			prepareParam[7] = operation;
			dss.executePrepareUpdate( sql.toString(), prepareParam );
			StringBuffer sql3 = new StringBuffer("delete from t_base_role_function where role_id="+Constants.SYSTEM_ROLE_ID);
			StringBuffer sql2 = new StringBuffer("INSERT INTO t_base_role_function" +
					"(role_id,res_id,oper_level) " +
			" select "+Constants.SYSTEM_ROLE_ID+",res_id,operation from t_base_function ");
			dss.executeUpdate(sql3.toString());
			n = dss.executeUpdate(sql2.toString());
			
			dss.endTransaction();
	
		}catch( SQLException sqle )
		{
			dss.rollback();
			throw sqle;
		}finally
		{
			dss.close();
		}
		return n;
	}
	
	public boolean update() throws SQLException
	{
		SQLUpdater updater = DSManager.getSQLUpdater();
		StringBuffer sql = new StringBuffer( "UPDATE t_base_function SET " )
			.append("res_name=?,")
			.append("node_type=?,")
			.append("model_id=?,")
			.append("position=?,")
//			.append("small_image=?,")
//			.append("big_image=?,")
			.append("link_value=?,")
			.append("operation=?,")
			.append("remark=?")
			.append(" WHERE res_id=?");
		Object [] prepareParam = new Object[8];
		prepareParam[0] = resName;
		prepareParam[1] = new Integer( nodeType );
		prepareParam[2] = new Integer( modelId );
		prepareParam[3] = new Integer( position );
//		prepareParam[4] = smallImage;
//		prepareParam[5] = bigImage;
		prepareParam[4] = linkValue;
		prepareParam[5] = operation;
		prepareParam[6] = description;
		prepareParam[7] = new Integer( resId );
		if( updater.executePrepareUpdate( sql.toString(), prepareParam) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public boolean delete() throws SQLException
	{
		if( resId <= 0 )
		{
			return false;
		}
		DSSession dss = DSManager.getDSSession();
		try
		{
			dss.beginTransaction();
			StringBuffer sql = new StringBuffer("DELETE FROM t_base_function WHERE res_id=?");
			StringBuffer sql2 = new StringBuffer("DELETE FROM t_base_role_function WHERE res_id=?");
			Object [] prepareParam = new Object[1];
			prepareParam[0] = new Integer( resId );
			if( dss.executePrepareUpdate( sql2.toString(), prepareParam ) > 0&&dss.executePrepareUpdate( sql.toString(), prepareParam ) > 0 )
			{
				dss.endTransaction();
				return true;
			}
		}
		catch( SQLException sqle )
		{
			dss.rollback();
			throw sqle;
		}finally
		{
			dss.close();
		}
		return false;
	}

	public boolean getById() throws SQLException
	{
		if( resId <= 0 )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT * FROM t_base_function where res_id=").append( resId );
		HashMap resourceInfo = query.getSingleRowResult( sql.toString() );
		if( resourceInfo != null )
		{
			parentId = StringHelper.getIntegerObjectValue( resourceInfo.get( "PARENT_ID" ) );
			resName = StringHelper.getStringObjectValue( resourceInfo.get("RES_NAME") );
			nodeType = StringHelper.getIntegerObjectValue( resourceInfo.get( "NODE_TYPE" ) );
			modelId = StringHelper.getIntegerObjectValue( resourceInfo.get( "MODEL_ID" ) );
			position = StringHelper.getIntegerObjectValue( resourceInfo.get( "POSITION" ) );
//			smallImage = StringHelper.getStringObjectValue( resourceInfo.get("SMALL_IMAGE") );
//			bigImage = StringHelper.getStringObjectValue( resourceInfo.get("BIG_IMAGE") );
			linkValue = StringHelper.getStringObjectValue( resourceInfo.get("LINK_VALUE") );
			description = StringHelper.getStringObjectValue( resourceInfo.get("RES_DESC") );
			operation = resourceInfo.get("OPERATION")+"";
			return true;
		}
		return false;
	}
	
	public boolean isDelete() throws SQLException
	{
		if( resId <= 0 )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_function WHERE parent_id=? ");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = new Integer( resId );
		if( query.getPrepareIntResult( sql.toString(), prepareParam ) > 0 )
		{
			return false;
		}
		return true;
	}
	
	public boolean exist() throws SQLException
	{
		if( resName==null || "".equals(resName) || "null".equals(resName) )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_function WHERE res_name=? ");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = resName;
		if ( query.getPrepareIntResult( sql.toString(), prepareParam ) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public int exist( int res_id ) throws SQLException
	{
		if( resId<=0 || resName==null || "".equals(resName) || "null".equals(resName) )
		{
			return 0;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_function WHERE res_name=? AND res_id<>? ");
		Object [] prepareParam = new Object[2];
		prepareParam[0] = resName;
		prepareParam[1] = new Integer( res_id );
		return query.getPrepareIntResult( sql.toString(), prepareParam );
	}
	
	public String getBigImage()
	{
		return bigImage;
	}

	public void setBigImage( String bigImage )
	{
		this.bigImage = bigImage;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription( String description )
	{
		this.description = description;
	}

	public String getLinkValue()
	{
		return linkValue;
	}

	public void setLinkValue( String linkValue )
	{
		this.linkValue = linkValue;
	}

	public int getModelId()
	{
		return modelId;
	}

	public void setModelId( int modelId )
	{
		this.modelId = modelId;
	}

	public int getNodeType()
	{
		return nodeType;
	}

	public void setNodeType( int nodeType )
	{
		this.nodeType = nodeType;
	}

	public int getParentId()
	{
		return parentId;
	}

	public void setParentId( int parentId )
	{
		this.parentId = parentId;
	}

	public int getPosition()
	{
		return position;
	}

	public void setPosition( int position )
	{
		this.position = position;
	}

	public int getResId()
	{
		return resId;
	}

	public void setResId( int resId )
	{
		this.resId = resId;
	}

	public String getResName()
	{
		return resName;
	}

	public void setResName( String resName )
	{
		this.resName = resName;
	}

	public String getSmallImage()
	{
		return smallImage;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public void setSmallImage( String smallImage )
	{
		this.smallImage = smallImage;
	}
}
